#include "mainWindow.h"
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <time.h>

mainWindow::mainWindow()
    :
    btnAdd("添加"),
    btnDelete("删除"),
    btnEdit("编辑")
{  
    std::cout<<"mainWindow PID: "<<getpid()<<std::endl;

    set_title("倒计时");
    set_size_request(300,300);

    //treeView
    refListStore=Gtk::ListStore::create(m_itemColumn);
    m_TreeView.set_model(refListStore);
    m_TreeView.append_column("标题",m_itemColumn.Title);
    m_TreeView.append_column("时间",m_itemColumn.Inf1);
    auto TVcolumn=m_TreeView.get_column(1);
    TVcolumn->set_min_width(120);

    //msw
    msw.set_policy(Gtk::PolicyType::AUTOMATIC,Gtk::PolicyType::AUTOMATIC);
    msw.set_expand(true);
    msw.set_child(m_TreeView);

    btnEdit.set_sensitive(false);
    btnDelete.set_sensitive(false);

    //boxButton
    boxButton.set_orientation(Gtk::Orientation::HORIZONTAL);
    boxButton.append(btnDelete);
    boxButton.append(btnAdd);
    boxButton.append(btnEdit);
    boxButton.set_spacing(20);
    boxButton.set_valign(Gtk::Align::CENTER);
    boxButton.set_expand(false);
    boxButton.set_halign(Gtk::Align::CENTER);

    //frame
    boxFrame.set_orientation(Gtk::Orientation::VERTICAL);
    boxFrame.append(msw);
    boxFrame.append(boxButton);
    set_child(boxFrame);

    InitSignal();

    try
    {
        cFile.readFileConfig();
    }
    catch(const int e)
    {
        std::cerr<<e<<std::endl;
    }

    for(auto i:cFile.m_timeData)
    {
        auto row=*(refListStore->append());
        row[m_itemColumn.Title]=i.Title;
        if(i.mode==0)
        {
            time_t resultTime=time(nullptr)-mktime(&(i.Time));
            char buf[512];
            std::sprintf(buf,"已经过%d天",resultTime/60/60/24);
            row[m_itemColumn.Inf1]=buf;
        }
        else if(i.mode==1)
        {
            time_t resultTime=mktime(&(i.Time))-time(nullptr);
            char buf[512];
            if (resultTime<-86400)
                std::sprintf(buf,"定时器已过期：%d-%d-%d",i.Time.tm_year+1900,i.Time.tm_mon+1,i.Time.tm_mday);
            else if(resultTime<=0)
                std::sprintf(buf,"还剩0天");
            else
                std::sprintf(buf,"还剩%d天",resultTime/60/60/24+1);
            row[m_itemColumn.Inf1]=buf;
        }
        row[m_itemColumn.node]=i.uuid;
    }
}

void mainWindow::InitSignal()
{
    btnAdd.signal_clicked().connect(sigc::mem_fun(*this,&mainWindow::on_btnAdd_Clicked));
    btnDelete.signal_clicked().connect(sigc::mem_fun(*this,&mainWindow::on_btnDelete_clicked));
    btnEdit.signal_clicked().connect(sigc::mem_fun(*this,&mainWindow::on_btnEdit_clicked));
    m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,&mainWindow::on_treeview_row_activate));
}

mainWindow::~mainWindow()
{
    try
    {
        cFile.writeFileConfig();
    }
    catch(const int e)
    {
        std::cerr<<e<<std::endl;
    }
}


void mainWindow::on_btnAdd_Clicked()
{
    refeditWindow.reset(new editWindow(0));
    refeditWindow->signal_finish().connect(sigc::mem_fun(*this,&mainWindow::on_editWindow_Filish));
    refeditWindow->show();
}

void mainWindow::on_btnDelete_clicked()
{
    auto refSelectRow=m_TreeView.get_selection();
    if(refSelectRow)
    {
        Gtk::TreeModel::iterator i=refSelectRow->get_selected();
        int selectNode=(*i)[m_itemColumn.node];
        refListStore->erase(i);
        auto listItr=cFile.m_timeData.begin();
        int ii=0;
        for(;ii<selectNode && listItr!=cFile.m_timeData.end();ii++ ,listItr++)
            ;
        cFile.m_timeData.erase(listItr);
    }

}

void mainWindow::on_btnEdit_clicked()
{
    refeditWindow.reset(new editWindow(1));
    refeditWindow->signal_finish().connect(sigc::mem_fun(*this,&mainWindow::on_editWindow_Filish));
    refeditWindow->show();
}

void mainWindow::on_editWindow_Filish(editRelease data,int mode)
{
    std::string newTitle=data.title;
    controlFile::timeData newTimeData;

    newTimeData.stringLength=newTitle.length();
    newTimeData.Title=newTitle;

    time_t targetTime=data.time.to_unix();
    time_t nowTime=time(nullptr);
    time_t releaseTime;

    tm newTime=*localtime(&targetTime);
    newTimeData.Time=newTime;

    if(targetTime>nowTime)
    {
        newTimeData.mode=1;
        releaseTime=targetTime-nowTime;
    }
    else
    {
        newTimeData.mode=0;
        releaseTime=nowTime-targetTime;
    }

    if(mode==0) //add
    {
        auto newRow=*(refListStore->append());
        newRow[m_itemColumn.Title]=data.title;
        newRow[m_itemColumn.node]=cFile.m_timeData.size();
        newTimeData.uuid=cFile.m_timeData.size();
        if(newTimeData.mode==0)
        {
            char buf[512];
            std::sprintf(buf,"已经过%d天",releaseTime/60/60/24);
            newRow[m_itemColumn.Inf1]=buf;
        }
        else
        {
            char buf[512];
            std::sprintf(buf,"还剩%d天",releaseTime/60/60/24+1);
            newRow[m_itemColumn.Inf1]=buf;
        }
        cFile.m_timeData.push_back(newTimeData);
    }
    else //edit
    {
        auto refSelection=m_TreeView.get_selection();
        if(refSelection)
        {
            auto i=refSelection->get_selected();
            int listNum;
            auto listItr=cFile.m_timeData.begin();

            (*i)[m_itemColumn.Title]=newTitle;
            if(newTimeData.mode==0)
            {
                char buf[512];
                std::sprintf(buf,"已经过%d天",releaseTime/60/60/24);
                (*i)[m_itemColumn.Inf1]=buf;
            }
            else
            {
                char buf[512];
                std::sprintf(buf,"还剩%d天",releaseTime/60/60/24+1);
                (*i)[m_itemColumn.Inf1]=buf;
            }

            int uuid=(*i)[m_itemColumn.node];
            for(;uuid!=listItr->uuid && listItr!=cFile.m_timeData.end();listItr++)
                ;
            *listItr=newTimeData;
        }
    }
}

void mainWindow::on_treeview_row_activate(const Gtk::TreeModel::Path &,Gtk::TreeViewColumn * column)
{
    if(column)
    {
        btnEdit.set_sensitive(true);
        btnDelete.set_sensitive(true);
    }
    else
    {
        btnEdit.set_sensitive(false);
        btnDelete.set_sensitive(false);
    }
}